package com.szjlc.torna.controller;

import com.szjlc.torna.entity.Customer;
import com.szjlc.torna.interceptor.ResponseAdvice;
import com.szjlc.torna.service.CustomerService;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.constraints.Min;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 客户前端控制层-torna.1.11.3
 * @author steve
 * @since 2022-11-19 22:09
 */
@Slf4j
@Validated
@RestController(value = "/customer")
public class CustomerController extends ResponseAdvice {

    @Resource
    private CustomerService customerService;

    @Resource
    private Environment environment;

    /** 通过id查询单个-torna.1.18.1 */
    @GetMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
    public Customer findById(@PathVariable("id") @Min(1) Integer id) {
        return customerService.findById(id);
    }

    /** 查询所有 */
    @GetMapping(value = "/list", produces = "application/json;charset=UTF-8")
    public List<Customer> list() {
        return customerService.findAll();
    }

    /** 保存单个 */
    @PostMapping(value = "/save", produces = "application/json;charset=UTF-8")
    public Customer save(@RequestBody @Validated Customer customer) {
        return customerService.insert(customer);
    }

    /** 批量保存 */
    @PostMapping(value = "/saveList", produces = "application/json;charset=UTF-8")
    public boolean saveList(@RequestBody @Validated List<Customer> customers) {
        log.info("customers = {}", customers);
        return true;
    }

    /** 更新单个 */
    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    public Customer update(@RequestBody @Validated Customer customer) {
        return customerService.update(customer);
    }

    /** 删除单个 */
    @PostMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    public boolean delete(@RequestBody @Validated Customer customer) {
        customerService.deleteById(customer.getCustomerId());
        return true;
    }


    /** 获取环境配置-重新启动服务 */
    @PostMapping(value = "/getEnvironmentInfo", produces = "application/json;charset=UTF-8")
    public String getEnvironmentInfo() {
        String[] activeProfiles = environment.getActiveProfiles();
        return Arrays.toString(activeProfiles);
    }


}
